package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.util.TmsFieldChecks;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.car.actionform.LicenseplateForm;
import com.tianqu.tms.www.car.controller.LicenseplateModifyResultController;
import com.tianqu.tms.www.car.viewhelper.CarStockModifyViewhelper;

public class LicenseplateModifyResultAction extends TmsAction{
    
	private static final String DETAIL ="licensePlatekModifyDetail"; 
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		LicenseplateForm inputForm = (LicenseplateForm)form; 
		CarStockModifyViewhelper viewhelper = new CarStockModifyViewhelper();
		viewhelper = (CarStockModifyViewhelper)uc.getAttribute(VIEWHELP);
		request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		//
		String gateType = Registry.getConfigFromDb(TmsConstants.GATE_TYPE);
		if("3".equals(gateType)){
			if(StringUtil.isNullOrBlank(inputForm.getmLicensePlate()) && StringUtil.isNullOrBlank(inputForm.getmTempLicensePlate()) ){
				setErrorsToRequest(request, TmsConstants.U8005 ,null);//车牌号码和易碎贴编号必须录入一个。
				return  mapping.findForward(DETAIL);
			}
		}else if("1".endsWith(gateType)){
			if(StringUtil.isNullOrBlank(inputForm.getmTempLicensePlate())){
				String[] param = {"易碎贴编号"};
				setErrorsToRequest(request, TmsConstants.U0005 ,param);
				return mapping.findForward(DETAIL);
			}
		}
        if(!StringUtil.isNullOrBlank(inputForm.getmLicensePlate()) && !TmsFieldChecks.isCarLicense(inputForm.getmLicensePlate()) ){
        	setErrorsToRequest(request, TmsConstants.U1002 ,null);
			return  mapping.findForward(DETAIL);
        }

		//check
		Car carNew = Car.getInstance();
		Member member = Member.getInstance();
		//保存按钮逻辑      
        carNew = carNew.findCarBySerial(inputForm.getSerial());
        if(carNew != null){
			if(carNew.getBarCodeNum() == 1){
				setErrorsToRequest(request, TmsConstants.U8001, null);//当前车辆被挂起，不能进行任何操作！
				viewhelper.setButtonFlag("false");
				request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
				return  mapping.findForward("licensePlatekModifyDetail");
			}
        }
		if(!StringUtil.isNullOrBlank(inputForm.getmLicensePlate()) ){
			carNew = carNew.findCarByLicensePlate(inputForm.getmLicensePlate(),1);
			if(!StringUtil.isNullOrBlank(carNew.getSerial())){
				member = member.findMemberInfoByMemId(String.valueOf(carNew.getMemberId()));
				String[] param = {member.getShortName()};
				setErrorsToRequest(request, TmsConstants.U1029 ,param);//车牌号码已经被登记使用，不能重复使用。
				return  mapping.findForward("licensePlatekModifyDetail");
			}
		} 
		if(!StringUtil.isNullOrBlank(inputForm.getmTempLicensePlate())){
			carNew = carNew.findCarByLicensePlate(inputForm.getmTempLicensePlate(),2);
			if(!StringUtil.isNullOrBlank(carNew.getSerial())){
				member = member.findMemberInfoByMemId(String.valueOf(carNew.getMemberId()));
				String[] param = {member.getShortName()};
				setErrorsToRequest(request, TmsConstants.U8008 ,param);	//临时车牌号码已经使用，不能重复使用.
				return  mapping.findForward("licensePlatekModifyDetail");
			}
		} 
				
		//
		viewhelper = new CarStockModifyViewhelper();
		viewhelper.setmLicensePlate(inputForm.getmLicensePlate());
		viewhelper.setmTempLicensePlate(inputForm.getmTempLicensePlate());
		viewhelper.setSerial(inputForm.getSerial());
		viewhelper.setIsPay(inputForm.getIsPay());
		viewhelper.setMemberId(inputForm.getMemberId());
		uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		//
		LicenseplateModifyResultController controller = new LicenseplateModifyResultController();
		controller.execute(uc);
		
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
